package com.tianqu.tms.www.cardect.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.entity.TmsDetectInfo;
import com.tianqu.tms.entity.TmsDetectPic;
import com.tianqu.tms.entity.TmsDetectSenior;

/**
 * 高级检测模式切换到普通检测模式
 * 
 * @author tianshifu
 * 
 */
public class CarWDPersonSeniorToNormalDetectResultController extends
		TmsController {
	private Logger logger = Logger
			.getLogger(CarWDPersonSeniorToNormalDetectResultController.class);// log
	private static final String CARSERIAL = "carSerial";// 车辆ID

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			logger.debug(this.getClass().getName() + "#execute was called.");

			String carSerial = (String) uc.getAttribute(CARSERIAL);// 车辆ID

			// 不要要查询数据库 直接清空uc就可以 清空uc的时候将车辆ID保留就可以了
			// 需要进入数据库进行查询看是否有检测信息,有的话清空
			TmsDetectInfo carInfo = TmsDetectInfo.getInstance();
			carInfo = carInfo.getWDIpadCarInfoBySerial(carSerial, 1);
			TmsDetectSenior senior = TmsDetectSenior.getInstance();
			TmsDetectPic pic = TmsDetectPic.getInstance();
			if (carInfo.getCarDetectId() > 0) {
				// 说明有检测报告，删之(高级检测报告)
				senior.delTisDetectSenior(carInfo.getCarDetectId());
				// 删除图片信息先查再删
				List<TmsDetectPic> picList = pic
						.findDetectPicInfoByDetectId(carInfo.getCarDetectId());

				if (picList != null) {
					pic.delPic(carInfo.getCarDetectId());// 删除所有检测图片信息
				}
				TmsUserProfile profile = uc.getUserProfile();
				// 更新基本信息表
				carInfo.resetCarInfoByIdAndStatus(carSerial, 3, profile
						.getUniqueId());
			}

			uc.clear();
			uc.setAttribute(CARSERIAL, carSerial);
			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
